package hu.tortavar2.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import hu.tortavar2.shared.Product;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;

public class ProductDao {

	DatastoreService datastore;
	private static final Logger log = Logger.getLogger(ProductDao.class.getName());
	
	public ProductDao() {
		datastore = DatastoreServiceFactory.getDatastoreService();
	}
	
	public List<Product> getRecentProducts(String category) {
		@SuppressWarnings("deprecation")
		Query query = new Query(Product.PRODUCT_ENTITY_NAME)
				.addFilter(Product.PRODUCT_CATEGORY, FilterOperator.EQUAL, category)
				.addFilter(Product.PRODUCT_ISACTIVE, FilterOperator.EQUAL, "on");
		log.info(category);
		query.addSort(Product.PRODUCT_NAME, SortDirection.ASCENDING);
		
		List<Product> results = new ArrayList<Product>();
		for (Entity result : datastore.prepare(query).asIterable()) {
			Product product = fromEntity(result);
			results.add(product);
		}
		return results;
	}
	
	public Product getProductByKey(String encodedKey) {
		Key key = KeyFactory.stringToKey(encodedKey);
		Entity result;
		try {
			result = datastore.get(key);
			Product product = fromEntity(result);
			return product;
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	
	private Product fromEntity(Entity result) {
		Product product = new Product();
		Map<String, Object> properties = result.getProperties();
		
		Iterator<Map.Entry<String, Object>> entries = properties.entrySet().iterator();
		while(entries.hasNext()) {
			Map.Entry<String, Object> entry = entries.next();
			if(entry.getKey().startsWith(Product.PRODUCT_DESC_2) || 
			   entry.getKey().startsWith(Product.PRODUCT_DESC_3) ||
			   entry.getKey().startsWith(Product.PRODUCT_DESC_4) ||
			   entry.getKey().startsWith(Product.PRODUCT_DESC_5)) {
				if (((String)entry.getValue()).trim().length() > 0)
					product.setProductDescX((String)entry.getValue());
			}
		}

		product.setProductName((String)result.getProperty(Product.PRODUCT_NAME));
		product.setProductPrice((String)result.getProperty(Product.PRODUCT_PRICE));
		product.setProductImageUrl((String)result.getProperty(Product.PRODUCT_IMAGE_URL));
		product.setWhereToBuy((String)result.getProperty(Product.PRODUCT_WHERE_TO_BUY));
		product.setProductDescription((String)result.getProperty(Product.PRODUCT_DESCRIPTION));
		product.setProductSpec((String)result.getProperty(Product.PRODUCT_DESC_SPEC));
		product.setProductCategory((String)result.getProperty(Product.PRODUCT_CATEGORY));
		product.setProductisActive((String)result.getProperty(Product.PRODUCT_ISACTIVE));
		
		if(product.getKey() == null) {
			String encodedKey = KeyFactory.keyToString(result.getKey());
			product.setKey(encodedKey);
		}
		return product;
	}
}
